package com.dcrzl.shcz.common.utils;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;

import java.util.Collection;
import java.util.Objects;


@Slf4j
public abstract class BaseController {

    @Autowired
    private HttpServletRequest request;

    private JSONObject parseJson() {
        if (StringUtils.hasText(request.getHeader("userToken")) && StringUtils.hasText(request.getHeader("token"))) {
            return JSONObject.parseObject(EncryptionUtil.getDecryptDESData(request.getHeader("userToken"), request.getHeader("token")));
        } else {
            return new JSONObject();
        }
    }

    protected Long getUserId() {
        JSONObject jsonObject = parseJson();
        return jsonObject.getLong("userId");
    }

    protected Long getRoleId() {
        JSONObject jsonObject = parseJson();
        return jsonObject.getLong("roleId");
    }

    protected Integer getUserType() {
        JSONObject jsonObject = parseJson();
        return jsonObject.getInteger("userType");
    }

    protected Collection<String> getGovUnitIdList1(){
        JSONObject jsonObject = parseJson();
        JSONArray govUnitIdList = jsonObject.getJSONArray("govUnitIdList1");
        if(Objects.nonNull(govUnitIdList)){
            return govUnitIdList.toJavaList(String.class);
        }
        return null;
    }
    protected Collection<String> getGovUnitIdList2(){
        JSONObject jsonObject = parseJson();
        JSONArray govUnitIdList = jsonObject.getJSONArray("govUnitIdList2");
        if(Objects.nonNull(govUnitIdList)){
            return govUnitIdList.toJavaList(String.class);
        }
        return null;
    }


}
